

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/SessionLoginServlet")
public class SessionLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static final String USERNAME = "mxs";
    private static final String PASSWORD = "123456";
	
	
    public SessionLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}


	public void destroy() {
		// TODO Auto-generated method stub
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        
        String username = request.getParameter("username");
        String password = request.getParameter("password");

      
        if (USERNAME.equals(username) && PASSWORD.equals(password)) {
            // 获取或创建 Session 对象
            HttpSession session = request.getSession();
          
            session.setAttribute("username", username);
           
            response.sendRedirect("sessionWelcome.jsp");
        } else {
           
            response.sendRedirect("sessionLogin.jsp");
        }
	}

}
